package com.bbs.controller;

import com.bbs.dto.AcceptNewFriendRequest;
import com.bbs.dto.ApplyNewFriendRequest;
import com.bbs.dto.FriendGroupDto;
import com.bbs.dto.UpdateFriendInfoRequest;
import com.bbs.pojo.Result;
import com.bbs.pojo.UserPrincipal;
import com.bbs.service.UserFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserFriendController {
    @Autowired
    UserFriendService friendService;

    /**
     * 获取好友列表
     * @param user
     * @return
     */
    @GetMapping("/friends")
    @PreAuthorize("isAuthenticated()")
    public Result<List<FriendGroupDto>> getGroupedFriendList(@AuthenticationPrincipal UserPrincipal user){
        return Result.succeed(friendService.getGroupedFriendList(user.getUserId()));
    }

    /**
     * 获取好友分组列表
     * @param user
     * @return
     */
    @GetMapping("/friend-groups")
    @PreAuthorize("isAuthenticated()")
    public Result<List<String>> getFriendGroupNames(@AuthenticationPrincipal UserPrincipal user){
        return Result.succeed(friendService.getFriendGroupNames(user.getUserId()));
    }

    /**
     * 同意好友添加申请
     * @param user
     * @param acceptNewFriendRequest
     * @return
     */
    @PostMapping("/friends/accept")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> acceptNewFriend(@AuthenticationPrincipal UserPrincipal user, @RequestBody AcceptNewFriendRequest acceptNewFriendRequest){
        return Result.build(friendService.acceptNewFriend(user.getUserId(), acceptNewFriendRequest));
    }

    /**
     * 拒绝好友添加申请
     * @param user
     * @param messageId
     * @return
     */
    @PostMapping("/friends/reject/{messageId}")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> rejectNewFriend(@AuthenticationPrincipal UserPrincipal user, @PathVariable("messageId") String messageId){
        return Result.build(friendService.rejectNewFriend(user.getUserId(), messageId));
    }

    /**
     * 申请添加好友
     * @param user
     * @param applyNewFriendRequest
     * @return
     */
    @PostMapping("/friends/apply")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> applyNewFriend(@AuthenticationPrincipal UserPrincipal user, @RequestBody ApplyNewFriendRequest applyNewFriendRequest){
        return Result.build(friendService.applyNewFriend(user, applyNewFriendRequest));
    }

    /**
     * 删除好友
     * @param user
     * @param friendUserId
     * @return
     */
    @DeleteMapping("/friends/{friendUserId}")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> deleteFriend(@AuthenticationPrincipal UserPrincipal user, @PathVariable String friendUserId){
        return Result.build(friendService.deleteFriend(user.getUserId(), friendUserId));
    }

    /**
     * 修改好友信息
     * @param user
     * @param updateFriendInfoRequest
     * @return
     */
    @PutMapping("/friends/info")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> updateFriendInfo(@AuthenticationPrincipal UserPrincipal user, @RequestBody UpdateFriendInfoRequest updateFriendInfoRequest){
        return Result.build(friendService.updateFriendInfo(user.getUserId(), updateFriendInfoRequest));
    }

    /**
     * 判断是否为好友
     */
    //内部调用（message服务使用）
    @GetMapping("/sys/friends/check/{userId1}/{userId2}")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> checkFriendship(@PathVariable("userId1") String userId1, @PathVariable("userId2") String userId2){
        return Result.withData(friendService.checkIsFriend(userId1, userId2));
    }
}
